package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.foolishbird.sdk.ui.activity.FoolishBirdFloatExtensionActivity;
import com.foolishbird.sdk.ui.activity.FoolishBirdPaymentActivity;
import com.foolishbird.sdk.ui.view.DialogFactory;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.ToolsUtils;


public class FoolishBirdIDVerifyFragment extends Fragment implements View.OnClickListener, TextWatcher {

    private EditText mIdentificationNameEt;
    private EditText mIdentificationCardEt;

    private Button mBindPhoneBtn;
    private ImageView mCloseWindowBtn;

    public static FoolishBirdIDVerifyFragment newInstance(Bundle bundle) {
        FoolishBirdIDVerifyFragment fragment = new FoolishBirdIDVerifyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_id_verify");
        View contentView = inflater.inflate(resource, null);
        contentView.findViewById(ResUtils.getViewIdentifier(getActivity(),"fb_fragment_question")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), FoolishBirdFloatExtensionActivity.class);

                intent.putExtra("custom",true);
                startActivity(intent);
            }
        });
        mIdentificationNameEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_username_edit"));
        mIdentificationCardEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_password_edit"));
        mBindPhoneBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_login_btn"));
        mBindPhoneBtn.setOnClickListener(this);
        mCloseWindowBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "fb_fragment_login_close"));
        mCloseWindowBtn.setOnClickListener(this);
        mCloseWindowBtn.setVisibility(View.GONE);
        mIdentificationNameEt.addTextChangedListener(this);
        mIdentificationCardEt.addTextChangedListener(this);
        mIdentificationCardEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE&&mBindPhoneBtn.isEnabled()){
                    doBindIdentityCardNumber();
                }
                Log.e("CodeEtOnKeyListener",actionId+".."+(event==null));
                return false;
            }
        });
        return contentView;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkSumbitButtonState();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onClick(View v) {
        if(v==mCloseWindowBtn){
            finishActivity();
        }else {
            doBindIdentityCardNumber();
        }

    }

    private void finishActivity() {
        FoolishBirdBindDialogFragment.OnRealNameVerifyListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doFinishActivity(this.getParentFragment());
        }
    }



    protected boolean isNullContext() {
        return getContext() == null;
    }

    private void checkSumbitButtonState() {
        String realName = mIdentificationNameEt.getText().toString().trim();
        String idCardNumber = mIdentificationCardEt.getText().toString().trim();

        if (TextUtils.isEmpty(realName) || TextUtils.isEmpty(idCardNumber)) {
            mBindPhoneBtn.setEnabled(false);
        } else {
            mBindPhoneBtn.setEnabled(true);
        }
    }

    private void doBindIdentityCardNumber() {
        String realName = mIdentificationNameEt.getText().toString().trim();
        String idCardNumber = mIdentificationCardEt.getText().toString().trim();

        if (TextUtils.isEmpty(realName)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_input_realname"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (TextUtils.isEmpty(idCardNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_idcard_number"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkIdentityCardNumber(idCardNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_idcard_correct"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        FoolishBirdBindDialogFragment.OnRealNameVerifyListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doBindOrRealNameVerify(null, null, realName, idCardNumber);
        }
    }

    private FoolishBirdBindDialogFragment.OnRealNameVerifyListener getOnRealNameVerifyListener() {
        Activity activity = getActivity();
        if (activity instanceof FoolishBirdBindDialogFragment.OnRealNameVerifyListener) {
            return (FoolishBirdBindDialogFragment.OnRealNameVerifyListener) activity;
        }
        return null;
    }


}
